package com.atguigu.juc;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


class Ticket{//资源类
    private int number=30;
    Lock lock=new ReentrantLock();//可重入锁，默认是非公平锁false。

    public void sale(){
        lock.lock();
        try {
            if(number>0){
                System.out.println(Thread.currentThread().getName()+"\t"+"卖出第："+(number--)+"\t还剩下"+number);
            }
        }finally {
            lock.unlock();
        }

    }

}

/**
 * @author ：MJS
 * @date ：Created in 2021/4/23  15:26
 * @description：三个售票员   卖出   30张票
 * 如何编写企业级需要的工程代码块？
 * 在高内聚低耦合的前提下 --    线程  操作   资源类
 *
 */
public class SaleTicketDemo {
    public static void main(String[] args) {//主方法  一切程序的入口
        Ticket ticket = new Ticket();

        //Thread(Runnable target, String name) Allocates a new Thread object.
        //      public abstract void run();
        new Thread(()->{ for (int i = 0; i <=31; i++) ticket.sale(); },"张三").start();
        new Thread(()->{ for (int i = 0; i <=31; i++) ticket.sale(); },"李四").start();
        new Thread(()->{ for (int i = 0; i <=31; i++) ticket.sale(); },"王五").start();


//Thread(Runnable target, String name) Allocates a new Thread object.
       /* new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i <=31; i++) {
                    ticket.sale();
                }
            }
        },"张三").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i <=31; i++) {
                    ticket.sale();
                }
            }
        },"李四").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i <=31; i++) {
                    ticket.sale();
                }
            }
        },"王五").start();*/
    }
}
